package rth.services.data;

import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;

import rth.services.proxies.TestSetStatusData;
import rth.utils.DAOException;

@Service
@RemotingDestination("homePageService")
public class HomePageService {

	private static Logger logger = Logger.getLogger(LoginService.class);

	private final DataLayerRth dataLayerRth;

	/**
	 * Constructor
	 */
	@Autowired
	public HomePageService(DataLayerRth dataLayerRth) {
		this.dataLayerRth = dataLayerRth;
	}

	public List<TestSetStatusData> getTopFiveTestSetWithStatus(long projectID) {
		List<TestSetStatusData> statusDataList = new ArrayList<TestSetStatusData>(); 
		try {
			
			Query query = dataLayerRth
					.createSQLQuery("SELECT ts.testsetid,ts.testsetname,b.BuildName,r.ReleaseName,cast(count(1) as SIGNED INTEGER) " +
							"TotalTests,sum(if(tsa.TestStatus = 'Passed', 1, 0)) Passed,sum(if(tsa.TestStatus = 'Failed', 1, 0)) Failed," +
							"sum(if(tsa.TestStatus = 'Incomplete', 1, 0)) Incomplete,sum(if(tsa.TestStatus = 'Finished : Awaiting Review', 1," +
							" 0)) far,sum(if(tsa.TestStatus = 'Not Started', 1, 0)) NotStarted,sum(if(tsa.TestStatus = 'WIP', 1, 0)) wip,ts.datecreated FROM testset ts,build b," +
							"release_tbl r,testset_testsuite_assoc tsa  WHERE   ts.buildid = b.buildid AND b.releaseid = r.releaseid " +
							"AND r.project_id = :projectId AND tsa.TestSetID = ts.TestSetID GROUP BY ts.testsetid,  ts.testsetname,  r.ReleaseName,  b.BuildName ORDER BY ts.datecreated DESC LIMIT 5");
			query.setLong("projectId", projectID);
			@SuppressWarnings("unchecked")
			List<Object[]> statusList = query.list();
			logger.debug("Size of the List in getTopFiveTestSetWithStatus method is "
					+ statusList != null ? statusList.size() : 0);
			
			
			for (Object[] object : statusList) {
				TestSetStatusData statusData = new TestSetStatusData();
				
				statusData.setTestSetId((Integer) object[0]);
				statusData.setTestSetName((String) object[1]);
				statusData.setBuildName((String) object[2]);
				statusData.setReleaseName((String) object[3]);
				statusData.setTotalTestCases(((BigInteger)object[4]).longValue());
				statusData.setPassedTestCases(((BigDecimal)object[5]).longValue());
				statusData.setFailedTestCases(((BigDecimal)object[6]).longValue());
				statusData.setIncompleteTestCases(((BigDecimal)object[7]).longValue());
				statusData.setFarTestCases(((BigDecimal)object[8]).longValue());
				statusData.setNotStartedTestCases(((BigDecimal)object[9]).longValue());
				statusData.setWipTestCases(((BigDecimal)object[10]).longValue());
				statusData.setDateReceived((String)object[11]);
								
				logger.debug("-->" + ReflectionToStringBuilder.toString(statusData));
				statusDataList.add(statusData);
			}
			
			

		} catch (Exception e) {
			logger.error("Exception while retreving user data from email", e);
			throw new DAOException(e);
		}
		
		return statusDataList;
	}

}
